/**
 * 离职通知
 * @author  Jello
 * @create  2018/10/19 10:58
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var sessionStorage = window.sessionStorage;
var spinner = new Spinner(opts);
var userid_sign = "";

$(function() {
	userid_sign = sessionStorage.getItem("userid_sign");

	//检测用户状态，若未在职，不支持离职通知
	var workstatus = sessionStorage.getItem("work_status");
	if(workstatus != 1) {
		$(".esc_divbox").show();
		$(".esc_box").show();
	}
	//提示框
	$(".tm_ckdeil").click(function() {
		window.location.href = "../../app/personal/personal.html";
	})

	backLogin(userid_sign, "../../login.html", function() {
		//先判断有没有离职通知
		$.ajax({
			type: "get",
			url: serverUrl + "/dimission/get_notice?sgin=" + userid_sign,
			cache: false,
			async: false,
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			crossDomain: true,
			timeout: 15000,
			beforeSend: function() {
				$("#myspin").text("");
				var target = $("#myspin").get(0);
				spinner.spin(target);
			},
			success: function(data) {
				spinner.spin();
				var resultObj = eval(data);
				if(resultObj.code == 0 && resultObj.data != null) {
					if(resultObj.data.isNotice == 1) { //1为有离职通知
						$(".leavetz_false").show();
						$(".leavetz_true").hide();
						$("#leave_info").html(resultObj.data.details);
					}
					if(resultObj.data.isNotice == 0) { //0为没有离职通知
						$(".leavetz_false").hide();
						$(".leavetz_true").show();
					}
				} else {
					mui.toast(resultObj.msg);
					return;
				}
			},
			error: function(XMLHttpRequest, status, errorThrown) {
				spinner.spin();
				if(status == 'timeout') {
					mui.toast("请求超时");
				} else {
					mui.toast("请求网络服务数据失败");
				}
				return;
			}
		});
	})

	//确定提交
	$("#leave_btn").click(function() {
		var reason = $("#textarea_html").val();
		if(reason == null || reason == "") {
			mui.toast("请输入离职原因和离职日期");
			return;
		} else {
			$.ajax({
				type: "get",
				url: serverUrl + "/dimission/add_dimission?sgin=" + userid_sign + "&reason=" + reason + "&type=" + 1,
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				timeout: 15000,
				dataType: "json",
				crossDomain: true,
				beforeSend: function() {
					$("#myspin").text("");
					var target = $("#myspin").get(0);
					spinner.spin(target);
				},
				success: function(data) {
					spinner.spin();
					var resultObj = eval(data);
					if(resultObj.code == 0 && resultObj.data != null) {
						if(resultObj.data == 1) {
							mui.toast("申请离职提交成功");
							$("textarea").val("").focus();
							setTimeout(function() {
								window.location.href = "../../app/personal/personal.html";
							}, 800);
						} else {
							mui.toast("提交失败");
							return;
						}
					} else {
						mui.toast(resultObj.msg);
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					spinner.spin();
					if(status == 'timeout') {
						mui.toast("请求超时");
					} else {
						mui.toast("请求网络服务数据失败");
					}
					return;
				}
			});
		}
	})

	//返回个人中心
	$("#cancel_tip").click(function() {
		window.location.href = "../../app/personal/personal.html";
	})

	//取消离职通知
	$("#cancel_btn").click(function() {
		var reason = $("#leave_info").html();
		$.ajax({
			type: "get",
			url: serverUrl + "/dimission/add_dimission?sgin=" + userid_sign + "&reason=" + reason + "&type=" + 2,
			cache: false,
			async: false,
			contentType: "application/json; charset=utf-8",
			timeout: 15000,
			dataType: "json",
			crossDomain: true,
			beforeSend: function() {
				$("#myspin").text("");
				var target = $("#myspin").get(0);
				spinner.spin(target);
			},
			success: function(data) {
				spinner.spin();
				var resultObj = eval(data);
				if(resultObj.code == 0 && resultObj.data != null) {
					if(resultObj.data == 1) {
						mui.toast("取消离职成功");
						setTimeout(function() {
							window.location.href = "../../app/personal/personal.html";
						}, 800);
					} else {
						mui.toast("取消失败");
						return;
					}
				} else {
					mui.toast(resultObj.msg);
					return;
				}
			},
			error: function(XMLHttpRequest, status, errorThrown) {
				spinner.spin();
				if(status == 'timeout') {
					mui.toast("请求超时");
				} else {
					mui.toast("请求网络服务数据失败");
				}
				return;
			}
		});
	})
})